package com.vids.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vids.mapper.ApeMapper;
import com.vids.model.entity.Ape;
import com.vids.model.param.ApeBatchModel;
import com.vids.model.param.ApeBatchParam;
import com.vids.service.IApeService;
import com.vids.utils.QueryUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 采集设备表 服务实现类
 * </p>
 *
 * @since 2019-06-06
 */
@Slf4j
@Service
public class ApeServiceImpl extends ServiceImpl<ApeMapper, Ape> implements IApeService {

    @Autowired
    private ApeMapper apeMapper;

    @Override
    public Ape getById(String id) {
	return apeMapper.getById(id);
    }

    @Override
    public void updateApe(Ape vo) {
	apeMapper.updateById(vo);
    }

    @Override
    @Transactional
    public String insertDevice(Ape ape) {
	String apeID = "";
	try {
		ape.setId(IdUtil.getSnowflakeNextIdStr());
	    apeMapper.insert(ape);
	    apeID = ape.getApeID();
	} catch (Exception e) {
	    log.error("保存设备信息失败：" + e);
	}
	return apeID;
    }

    @Override
    public ApeBatchParam getDeviceList(Map<String, Object> map) {
	// 无条件查询默认返回所有
	if (map == null || map.size() == 0) {
	    List<Ape> resultList = apeMapper.findAll();
	    return buildResultModel(resultList);
	}
	Map<String, Object> paramMap = QueryUtils.buildParam(map);
	List<Ape> resultList = apeMapper.getAnalysisRuleList(paramMap);
	return buildResultModel(resultList);
    }

    private ApeBatchParam buildResultModel(List<Ape> resultList) {
	if (resultList != null && resultList.size() > 0) {
	    ApeBatchParam param = new ApeBatchParam();
	    ApeBatchModel batchModel = new ApeBatchModel();
	    batchModel.setAPEObject(resultList);
	    param.setAPEListObject(batchModel);
	    return param;
	}
	return null;
    }

}
